package com.fl.nj.controller;

import com.fl.nj.entity.DatabaseConfig;
import com.fl.nj.service.DatabaseConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.HashMap; // 添加缺失的HashMap导入

/**
 * 数据库配置Controller
 * 提供RESTful API接口，供Vue前端调用
 */
@RestController
@RequestMapping("/api/database-config")
@CrossOrigin(origins = "*", allowedHeaders = "*", methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.PUT, RequestMethod.DELETE, RequestMethod.OPTIONS})
public class DatabaseConfigController {
    @Autowired
    private DatabaseConfigService databaseConfigService;

    /**
     * 获取所有数据库配置列表
     * @return 数据库配置列表
     */
    @GetMapping
    public ResponseEntity<List<DatabaseConfig>> getAll() {
        List<DatabaseConfig> configList = databaseConfigService.getAll();
        return ResponseEntity.ok(configList);
    }

    /**
     * 根据ID获取数据库配置
     * @param id 配置ID
     * @return 数据库配置对象
     */
    @GetMapping("/{id}")
    public ResponseEntity<DatabaseConfig> getById(@PathVariable Long id) {
        DatabaseConfig config = databaseConfigService.getById(id);
        if (config == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(config);
    }

    /**
     * 根据配置名称获取数据库配置
     * @param configName 配置名称
     * @return 数据库配置对象
     */
    @GetMapping("/name/{configName}")
    public ResponseEntity<DatabaseConfig> getByConfigName(@PathVariable String configName) {
        DatabaseConfig config = databaseConfigService.getByConfigName(configName);
        if (config == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(config);
    }

    /**
     * 添加数据库配置
     * @param config 数据库配置对象
     * @return 添加结果
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> save(@RequestBody DatabaseConfig config) {
        Map<String, Object> result = new HashMap<>();
        try {
            databaseConfigService.save(config);
            result.put("success", true);
            result.put("message", "添加成功");
            result.put("data", config);
            return ResponseEntity.status(HttpStatus.CREATED).body(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    /**
     * 更新数据库配置
     * @param id 配置ID
     * @param config 数据库配置对象
     * @return 更新结果
     */
    @PutMapping("/{id}")
    public ResponseEntity<Map<String, Object>> update(@PathVariable Long id, @RequestBody DatabaseConfig config) {
        config.setId(id);
        Map<String, Object> result = new HashMap<>();
        try {
            databaseConfigService.update(config);
            result.put("success", true);
            result.put("message", "更新成功");
            result.put("data", config);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }

    /**
     * 删除数据库配置
     * @param id 配置ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> delete(@PathVariable Long id) {
        Map<String, Object> result = new HashMap<>();
        try {
            databaseConfigService.delete(id);
            result.put("success", true);
            result.put("message", "删除成功");
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(result);
        }
    }
}